package com.share.example.selenium.service.impl;

import com.alibaba.fastjson.JSON;
import com.share.common.utils.DelayUtil;
import com.share.example.selenium.service.AbsDriver;
import com.share.example.selenium.service.CommonService;
import com.share.example.selenium.service.IBaseConfService;
import com.share.example.selenium.utils.DriverUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class CommomServiceImpl implements CommonService {
    @Autowired
    public IBaseConfService baseConfService;

    @Autowired
    public ApplicationContext applicationContext;

    private WebDriver getDriverByCode(String processCode) {
        return DriverUtil.getDriver(processCode);
    }

    //重新注入
    @Override
    public Boolean reInitDriver(String processCode) {
        WebDriver tmpDriver = DriverUtil.reInitDriver(processCode);
        Map<String, AbsDriver> beansOfType = applicationContext.getBeansOfType(AbsDriver.class);
        //初始化所有的bean
        beansOfType.values().stream().forEach(e -> e.reInitDriver(tmpDriver));

        if (tmpDriver == null) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 1.driver.navigate().refresh();
     * 2.driver.get(driver.getCurrentUrl());
     * 3.driver.navigate().to(driver.getCurrentUrl());
     * 4.driver.findElement(By.id("Contact-us")).sendKeys(Keys.F5);
     * 5.driver.executeScript("history.go(0)");
     *
     * @param delaySecord
     * @param processCode
     * @return
     */
    @Override
    public Boolean refreshChrome(int delaySecord, String processCode) {
        DriverUtil.getDriver(processCode).navigate().refresh();
        DelayUtil.sleepSecond(delaySecord);
        return true;
    }

    @Override
    public Object getCookies(String processCode) {
        Set<Cookie> cookies = DriverUtil.getDriver(processCode).manage().getCookies();
        return JSON.toJSON(cookies);
    }

    @Override
    public Integer setOneCookie(String processCode, String cookieStr) {
        WebDriver.Options manage = DriverUtil.getDriver(processCode).manage();
        Cookie cookie = JSON.parseObject(cookieStr, Cookie.class);
        manage.addCookie(cookie);
        return 1;
    }

    @Override
    public Integer loadCookies(String processCode, String cookieSetStr) {
        WebDriver.Options manage = DriverUtil.getDriver(processCode).manage();
        List<Cookie> cookies = JSON.parseArray(cookieSetStr, Cookie.class);
        cookies.forEach(c -> manage.addCookie(c));
        return cookies.size();
    }

    @Override
    public boolean openUrl(String url, int delaySecord, String processCode) {
        try {
            getDriverByCode(processCode).get(url);
            DelayUtil.sleepSecond(2);
            return true;
        } catch (Exception e) {
            log.error("打开地址异常", e);
            return false;
        }
    }

    @Override
    public Set<String> listWindows(String processCode) {
        return DriverUtil.listWindows(getDriverByCode(processCode));
    }

    @Override
    public boolean switchToTarget(String desTarget, String processCode) {
        WebDriver driverByCode = getDriverByCode(processCode);
        String windowHandle1 = driverByCode.getWindowHandle();
        DriverUtil.switchToTarget(driverByCode, desTarget, null);
        String windowHandle2 = driverByCode.getWindowHandle();

        if (windowHandle2.equalsIgnoreCase(desTarget)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Object exeGroovyByCode(String code, String processCode) {
        String groovy = baseConfService.getGroovyByCodeOrId(code, null);
        return baseConfService.exeGroovy(groovy, getDriverByCode(processCode));
    }

    @Override
    public Object exeGroovyById(String id, String processCode) {
        String groovy = baseConfService.getGroovyByCodeOrId(null, id);
        return baseConfService.exeGroovy(groovy, getDriverByCode(processCode));
    }

    @Override
    public boolean closeTab(String desTarget, String processCode) {
        try {
            WebDriver driverByCode = getDriverByCode(processCode);
            switchToTarget(desTarget, processCode);
            DriverUtil.closeTab(driverByCode);
            return true;
        } catch (Exception e) {
            log.error("关闭tab异常", e);
            return false;
        }
    }
}
